In [2]:
from math import sqrt
def fibo1(n): # Recursive Fibonacci number
if n == 0:
return 0
elif n == 1:
return 1
return fibo1(n-1) + fibo1(n-2)
def fibo2(n): # Closed Form
return ((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5))
In [3]:
%timeit fibo1(20)
In [4]:
%timeit fibo2(20)
In [6]:
import random
In [7]:
%%timeit
prize = 0
for ii in range(100):
# roll a die
roll = random.randint(1, 6)
if roll % 2 == 0:
prize += roll
else:
prize -= 1
In [8]:
%%timeit
rolls = (random.randint(1, 6) for _ in range(100))
prize = sum(roll if roll % 2 == 0 else -1 for roll in rolls)
In [10]:
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
import matplotlib.pyplot as plt
import numpy as np
In [11]:
x = np.linspace(0, 1, 300)
for w in range(2, 6, 2):
plt.plot(x, np.sin(np.pi*x) * np.sin(2*w*np.pi*x))
In [12]:
%pdb
In [ ]:
numbers = 'hello'
sum(numbers)